package com.zxy.springboot_vue.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxy.springboot_vue.config.AjaxResult;
import com.zxy.springboot_vue.entity.Role;
import com.zxy.springboot_vue.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lenovo
 * @since 2021-10-19
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @RequestMapping("getRoles")
    public AjaxResult getRole(Role role, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3")Integer pageSize){
        QueryWrapper<Role> wrapper = null;
        PageHelper.startPage(pageNum,pageSize);
        if(role.getNameZh()!=null){
            wrapper = new QueryWrapper<>();
            wrapper.like("nameZh",role.getNameZh());
        }
        List<Role> list = roleService.list(wrapper);
        PageInfo<Role> info = new PageInfo<>(list);
        return AjaxResult.ok(info);
    }


    @RequestMapping("getRole")
    public AjaxResult getRole(Role role){
        QueryWrapper<Role> wrapper = null;
        if(role.getNameZh()!=null){
            wrapper = new QueryWrapper<>();
            wrapper.like("nameZh",role.getNameZh());
        }
        List<Role> list = roleService.list(wrapper);
        return AjaxResult.ok(list);
    }

    @PostMapping("addRole")
    public AjaxResult addRole(@RequestBody Role role){
        role.setCreated(LocalDateTime.now());
        boolean b = roleService.saveOrUpdate(role);
        return AjaxResult.ok();
    }

    @PostMapping("delBeach")
    public AjaxResult delBeach(@RequestBody Role role){

        boolean b = roleService.removeByIds(role.getIds());
        return AjaxResult.ok();
    }

    @RequestMapping("getRoleById")
    public AjaxResult getRoleById(Role role){
        System.out.println(role);
        Role roles = roleService.getById(role.getId());
        return AjaxResult.ok(roles);
    }


}
